package com.chopperdrop;

public class ball
{
	public final static double acell_decay_rate = 0.6;
	public final static double acell_rate = .1;
	protected volatile float xpos;
	protected volatile float ypos;
	protected volatile float xvel;
	protected volatile float yvel;
	protected volatile float xacel;
	protected volatile float yacel;
	protected volatile float xmax;
	protected volatile float ymax;
	protected volatile float xmin;
	protected volatile float ymin;
	protected float rad;
	protected volatile long last_update;
	protected int width;
	protected int height;
	
	public ball()
	{
        xpos = 75;
        ypos = 35;
        xvel = 0;
        yvel = 2;
        xacel = 0;
        yacel = 20;
        rad = 3;
        width = 10;
        height = 10;
        xmax =100;
        xmin =0;
        ymax =150;
        xmin =0;
        
        last_update = System.currentTimeMillis();
	}
	public ball(float initX, float initY)
	{
        xpos = initX;
        ypos = initY;
        xvel = 0;
        yvel = 2;
        xacel = 0;
        yacel = 20;
        rad = 3;
        width = 11;
        height = 20;
        xmax =100;
        xmin =0;
        ymax =150;
        xmin =0;
        
        last_update = System.currentTimeMillis();
	}
	
	public void setVelocity(float ax, float ay) 
    {
        xvel = ax;
        yvel= ay;
    }
    public void setPosition(float ax, float ay) 
    {
        xpos = ax;
        ypos= ay;
    }
    
    
    
    public int update()
	{
    	int ret_val=2;
    	long current_time = System.currentTimeMillis();
    	long time_diff = current_time - last_update;    
		Float newxpos = xpos;
		Float newypos = ypos;
		Float newxvel = xvel;
		Float newyvel = yvel;

		
		newxpos += (xvel * time_diff)/100;
		newypos += (yvel * time_diff)/100;
		        
		newxvel += ((time_diff * xacel) / 100000);
		newyvel += ((time_diff * yacel) / 100000);


		if (newxpos-rad < xmin)
		{
			xacel *=.7;
			newxpos += (xmin-(newxpos-rad));
			newxvel *= -1;
			ret_val = 0;	
			if(!Playtime.didCollide)
			{
				Playtime.didCollide=true;
				Playtime.collidedAt='l';
				Playtime.xcol=xpos;
				Playtime.ycol=ypos;
			}
		}
		if (newxpos+rad > xmax)
		{
			xacel *=.7;
			newxpos -= (newxpos + rad)-xmax;
			newxvel *= -1;
			ret_val = 0;
			if(!Playtime.didCollide)
			{
				Playtime.didCollide=true;
				Playtime.collidedAt='r';
				Playtime.xcol=xpos;
				Playtime.ycol=ypos;
			}
		}
		
		if (newypos-rad < ymin)
		{
			ret_val = 1;
		}
		if (newypos+rad > ymax)
		{
			ret_val = -1;
		}
		xpos = newxpos;
		ypos = newypos;
		xvel = newxvel;
		yvel = newyvel;

		last_update = current_time;
		return ret_val;
	}

}